package com.roast.domain;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

@SuppressWarnings("serial")
@Entity
public class Event extends DomainObject {

    @Id
    @GeneratedValue
    private long eventID;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "masterID")
    private UserData master;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "starID")
    private UserData star;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "forewordID")
    private Text foreword;

    private boolean isOpen;

    private boolean isPrivate;

    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinTable(name = "event_userdata", joinColumns = {
        @JoinColumn(name = "eventID")}, inverseJoinColumns = {
        @JoinColumn(name = "userID")})
    private List<UserData> participantsList;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Address> addressList;

    public long getEventID() {
        return eventID;
    }

    public void setEventID(long eventID) {
        this.eventID = eventID;
    }

    public UserData getMaster() {
        return master;
    }

    public void setMaster(UserData master) {
        this.master = master;
    }

    public UserData getStar() {
        return star;
    }

    public void setStar(UserData star) {
        this.star = star;
    }

    public Text getForeword() {
        return foreword;
    }

    public void setForeword(Text foreword) {
        this.foreword = foreword;
    }

    public boolean getIsOpen() {
        return isOpen;
    }

    public void setIsOpen(boolean isOpen) {
        this.isOpen = isOpen;
    }

    public boolean getIsPrivate() {
        return isPrivate;
    }

    public void setIsPrivate(boolean isPrivate) {
        this.isPrivate = isPrivate;
    }

    public List<UserData> getParticipantsList() {
        return participantsList;
    }

    public void setParticipantsList(List<UserData> participantsList) {
        this.participantsList = participantsList;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    //IDomainObject
    @Override
    public long getId() {
        return eventID;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Class objectClass() {
        return Event.class;
    }

    @Override
    public boolean copyObject(Object from) {
        try {
            Event database = (Event) from;

            this.eventID = database.getEventID();
            this.master = database.getMaster();
            this.star = database.getStar();
            this.foreword = database.getForeword();
            this.isOpen = database.getIsOpen();
            this.isPrivate = database.getIsPrivate();
            this.participantsList = database.getParticipantsList();
            this.addressList = new ArrayList<>(database.getAddressList());

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public String toString() {
        String text = "Event [eventID:" + eventID + "]"
                + "\n" + "Master [" + master.toString() + "]"
                + "\n" + "Star [" + star.toString() + "]"
                + "\n" + "Foreword [" + foreword.toString() + "]"
                + "\n" + "isOpen:" + isOpen
                + "\n" + "isPrivate:" + isPrivate
                + "\n" + "ParticioantsList:";

        for (int i = 0; i < this.participantsList.size(); i++) {
            text += "\n" + this.participantsList.get(i).toString();
        }

        text += "\n" + "addressList:";
        for (int i = 0; i < this.addressList.size(); i++) {
            text += "\n" + this.participantsList.get(i).toString();
        }
        return text;
    }

    //ParticipantsNicknames premesti u kontroler gde se koristi
    @Transient
    public String getParticipantsNicknames() {
        String participants = "";
        for (int i = 0; i < this.participantsList.size(); i++) {
            UserData user = this.participantsList.get(i);
            participants += user.getNickname();
            if (i != this.participantsList.size() - 1) {
                participants += " | ";
            }
        }
        return participants;
    }
}
